package com.ruoyi.web.controller.basedata;

import com.ruoyi.basedata.service.IDwAreasService;
import com.ruoyi.basedata.service.ITerminalsDbService;
import com.ruoyi.basedata.wxentity.Terminals;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.WeChatTableDataInfo;
import com.ruoyi.datastatistics.service.IRealtimegatherdataService;
import com.ruoyi.datastatistics.wxentity.MapTerminalInfo;
import com.ruoyi.web.convert.area.AreaConvert;
import com.ruoyi.web.entity.Area;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 大档案Controller
 *
 * @author ruoyi
 * @date 2023-09-07
 */
@RestController
@RequestMapping("/api/TerminalsManagement")
public class TerminalsDbController extends BaseController
{
    @Autowired
    private ITerminalsDbService terminalsDbService;

    @Autowired
    private IDwAreasService dwAreasService;

    @Autowired
    private IRealtimegatherdataService realtimegatherdataService;


    @GetMapping(value = "/getAreaTreeForMobile")
    public WeChatTableDataInfo getAreaTree(String operatorId,String areaId)
    {
        Long areaId_ = areaId==null||areaId.equals("")? null :  Long.parseLong(areaId);
        PageDomain pageDomain = startWeChatPage();
        List<Area> list = AreaConvert.INSTANCE.convert(dwAreasService.childrenList(areaId_,null));
       /* list.forEach(e->{

        });*/
        return getWeChatDataTable(list,pageDomain);
    }



    /**
     * 查询实时数据采集（大）列表
     */
    @GetMapping("/searchTerminalByName")
    public WeChatTableDataInfo list(@RequestParam(value = "TerminalID",required = false) String TerminalID,
                              @RequestParam(value = "terminalName",required = false)String TerminalName,
                              @RequestParam(value = "areaid",required = false) String areaid,
                              @RequestParam(value = "startTime",required = false) String startTime,
                              @RequestParam(value = "endTime",required = false) String endTime
    )
    {
        /*if(null == startTime || startTime.isEmpty() || null == endTime || endTime.isEmpty()){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            endTime = simpleDateFormat.format(new Date());
            startTime = DateUtil.getLastDayEndDate(endTime);
        }*/
        PageDomain pageDomain = startWeChatPage();
        List<MapTerminalInfo> list = realtimegatherdataService.SearchTerminal(TerminalID,TerminalName,areaid,startTime,endTime);
        return getWeChatDataTable(list,pageDomain);
    }



    /**
     * 地图监测根据设备名称查询历史数据
     */
    @GetMapping("/SearchTerminal")
    public WeChatTableDataInfo SearchTerminal(@RequestParam(value = "TerminalID",required = false) String TerminalID,
                                              @RequestParam(value = "keyword",required = false)String TerminalName,
                                              @RequestParam(value = "areaid",required = false) String areaid,
                                              @RequestParam(value = "startTime",required = false) String startTime,
                                              @RequestParam(value = "endTime",required = false) String endTime
    )
    {
        PageDomain pageDomain = startWeChatPage();
        List<MapTerminalInfo> list = realtimegatherdataService.SearchTerminal(TerminalID,TerminalName,areaid,startTime,endTime);
        return getWeChatDataTable(list,pageDomain);
    }

    /**
     * 设备详情
     * @param id
     * @return
     */
    @GetMapping("/GetTerminalInfoById")
    public WeChatTableDataInfo getInfo(@RequestParam("id") Long id)
    {
        PageDomain pageDomain = startWeChatPage();
        List<Terminals> terminalList = terminalsDbService.selectWXTerminalsDbById(id);
        return getWeChatDataTable(terminalList,pageDomain);
    }







}
